import networkx as nx
import matplotlib.pyplot as plt

# 有向图
G=nx.DiGraph()
G.add_node(1)
G.add_node(2)
# 添加点集合
G.add_nodes_from([3,4,5,6])
G.add_cycle([1,2,3,4])
G.add_edge(1,3)
# 添加边集合
G.add_edges_from([(3,5),(3,6),(6,7)])
# 有向图与无向图的转化
G=G.to_undirected()
G=G.to_directed()
nx.draw(G,with_labels=True)
plt.show()